package com.longge;

import java.util.Random;

class Solution {

    private ListNode head;
    /**
     * @param head The linked list's head.
     *             Note that the head is guaranteed to be not null, so it contains at least one node.
     */
    public Solution(ListNode head) {
        this.head = head;
    }

    /**
     * Returns a random node's value.
     */
    public int getRandom() {
        int count = 0, res = 0;
        ListNode temp = head;
        Random random = new Random();
        while (temp != null) {
            count++;
            if ((random.nextInt(count) + 1) / count == 1) {
                res = temp.val;
            }
            temp = temp.next;
        }
        return res;
    }

    public static void main(String[] args) {
        System.out.println(Math.random() + 1);
    }
}